﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Sinacor.Infra.Core.ProcessManager.Client.UI
{
    /// <summary>
    /// Representa uma exceção gerada na comunicação com os serviços
    /// </summary>
    public class ProcessCommunicationException : Sinacor.Infra.UI.Exceptions.CommunicationException
    {
        private const int PROCESS_ENDPOINT_NOT_FOUND_ERROR = 20005;
        private const int PROCESS_TIMEOUT_ERROR = 20004;
        private const int PROCESS_COMMUNICATION_ERROR = 20003;

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public ProcessCommunicationException()
            : this(null, null)
        {
        }

        /// <summary>
        /// Construtor que inicializa InnerException
        /// </summary>
        /// <param name="innerException">Erro que originou a Exception</param>
        public ProcessCommunicationException(Exception innerException)
            : this(null, innerException)
        {
        }

        /// <summary>
        /// Construtor que inicializa Message e InnerException
        /// </summary>
        /// <param name="message">Mensagem de erro</param>
        /// <param name="innerException">Erro que originou a Exception</param>
        public ProcessCommunicationException(string message, Exception innerException)
            : base(message, innerException)
        {
            if (this.InnerException is EndpointNotFoundException)
            {
                // Não foi possível se conectar com o serviço
                this.ErrorNumber = PROCESS_ENDPOINT_NOT_FOUND_ERROR;
                this.ResourceKey = "Common.Application.ExceptionMessage.ProcessEndpointNotFound";

            }
            else if (this.InnerException is TimeoutException)
            {
                // Timeout
                this.ErrorNumber = PROCESS_TIMEOUT_ERROR;
                this.ResourceKey = "Common.Application.ExceptionMessage.ProcessTimeout";
            }
            else
            {
                // Outros erros
                this.ErrorNumber = PROCESS_COMMUNICATION_ERROR;
                this.ResourceKey = "Common.Application.ExceptionMessage.ProcessCommunicationFailed";
            }
        }
    }
}
